﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlotasDesktop;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmAuto
{
    public partial class ModificacionAuto : Form
    {
        String conexion;
        String idAuto;
        public ModificacionAuto()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
            this.rellenarComboBoxMarcas();
            this.rellenarComboBoxReloj();
        }

        public void mostrateConCampos(String idAuto, String licencia, String rodado, String id_marca, String modelo, String patente, String id_reloj, Boolean habilitado)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        String marca;
                        String relojNroSerie;
                        this.idAuto = idAuto;
                        marca = this.buscarMarca(id_marca);
                        relojNroSerie = this.buscarReloj(id_reloj);

                        marcaComboBox.Text = marca;
                        
                        licenciaTextBox.Text = licencia;
                        modeloTextBox.Text = modelo;
                        patenteTextBox.Text = patente;
                        rodadoTextBox.Text = rodado;
                        relojComboBox.Text = relojNroSerie;
                        habilitadoCheckBox.Checked = habilitado;
                        this.Show();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        public void modificarCampos()
         {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        String marca;
                        String relojNroSerie;
                                               
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_modificarAutomovil";
                        command.Parameters.AddWithValue("@Id",this.idAuto);
                        command.Parameters.AddWithValue("@Marca", marcaComboBox.Text);
                        command.Parameters.AddWithValue("@Licencia", licenciaTextBox.Text);
                        command.Parameters.AddWithValue("@Rodado", rodadoTextBox.Text);
                        command.Parameters.AddWithValue("@Modelo", modeloTextBox.Text);
                        command.Parameters.AddWithValue("@Patente", patenteTextBox.Text);
                        command.Parameters.AddWithValue("@Reloj_nro_serie", relojComboBox.Text);
                        command.Parameters.AddWithValue("@Habilitado", habilitadoCheckBox.Checked);
                        command.ExecuteReader();
                        connection.Close();
                        MessageBox.Show("Modificacion realizada con exito");
                        this.Close();
                       
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
             }
          }
        

        private void button1_Click(object sender, EventArgs e)
        {
            if (marcaComboBox.Text != string.Empty && licenciaTextBox.Text != string.Empty && relojComboBox.Text != string.Empty && patenteTextBox.Text != string.Empty && rodadoTextBox.Text != string.Empty)
            {
                this.modificarCampos();
            }
            else
            {
                MessageBox.Show("Todos los campos deben estar completos", "Error");
            }
        }

 
        private String buscarMarca(String id_marca)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_marca_con_id";
                        command.Parameters.AddWithValue("@id", id_marca);
                        SqlParameter countParameter = new SqlParameter("@retorno",SqlDbType.NVarChar,255);
                        countParameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(countParameter);
                        command.ExecuteNonQuery();
                        connection.Close();
                        return command.Parameters["@retorno"].Value.ToString();

                    }
                }
                catch (Exception ex)
                {
                    return "";
                }

            }

        }

        private String buscarReloj(String id_reloj)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_buscarReloj2";
                        command.Parameters.AddWithValue("@id", id_reloj);
                        SqlParameter countParameter = new SqlParameter("@retorno", SqlDbType.NVarChar, 255);
                        countParameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(countParameter);
                        command.ExecuteNonQuery();
                        connection.Close();
                        return command.Parameters["@retorno"].Value.ToString();

                    }
                }
                catch (Exception ex)
                {
                    return "";
                }

            }

        }
        
        public void rellenarComboBoxMarcas()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_marca_automovil";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            marcaComboBox.DataSource = dt;
                            marcaComboBox.DisplayMember = "descripcion";

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al cargar marcas de automovil");
                }
            }
        }

        public void rellenarComboBoxReloj()
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_seleccionar_reloj";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            adapter.Fill(dt);
                            relojComboBox.DataSource = dt;
                            relojComboBox.DisplayMember = "nro_serie";

                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    
    }
}
